﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Openapi.Weibo;
using System.Web.Routing;
using System.Web.Security;

namespace Sample.Mvc3.Controllers
{
    public class HomeController : ControllerBase
    {
        public ActionResult Index()
        {
            ViewBag.Message = "欢迎使用 Openapi Mvc Demo!";
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [HttpGet]
        public ActionResult CallBack(string code,string state)
        {
            OAuthAuthorization oauth = new OAuthAuthorization();
            var token = oauth.GetAccessTokenByCode(code);
            var client = new Client(token);
            var statuses = client.Statuses.PublicTimeline();
            if (client.VerifyCredentials(token.access_token))
            {
                Session["accesstoken"] = token;
                Session["UserInfo"] = new Models.UserinfoModel { UserName="Michael明少" };
                FormsAuthentication.SetAuthCookie(token.uid, false /* createPersistentCookie */);
                if (String.IsNullOrEmpty(state))
                {
                    ViewBag.RedirectUrl = "/Weibo/Index";
                    return View();
                }
                return RedirectToAction("Index", "Weibo");
            }
            return RedirectToAction("Index");
        }
    }
}
